package cn.edu.gxu.xingjian.teacherassistant.domain;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.PersistenceContext;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.transaction.annotation.Transactional;

@Configurable
@Entity
@RooJavaBean
@RooToString
@RooJpaActiveRecord
public class LessonPlans {

    @NotNull
    @Size(min = 1, max = 3)
    private String totalHours;

    @Size(max = 3)
    private String curricularHours;

    @Size(max = 3)
    private String extracurricularHours;

    @Size(max = 3)
    private String experiment;

    @Size(max = 3)
    private String otherHours;

    @Size(min = 1, max = 2)
    private String weeklyHours;

    @ManyToOne
    private Major major;

    @ManyToOne
    private ExaminationWay examinationWay;

    @ManyToOne
    private Curriculum curriculum;

    @ManyToMany(cascade = CascadeType.ALL)
    private Set<Semester> semester = new HashSet<Semester>();

	public String getTotalHours() {
        return this.totalHours;
    }

	public void setTotalHours(String totalHours) {
        this.totalHours = totalHours;
    }

	public String getCurricularHours() {
        return this.curricularHours;
    }

	public void setCurricularHours(String curricularHours) {
        this.curricularHours = curricularHours;
    }

	public String getExtracurricularHours() {
        return this.extracurricularHours;
    }

	public void setExtracurricularHours(String extracurricularHours) {
        this.extracurricularHours = extracurricularHours;
    }

	public String getExperiment() {
        return this.experiment;
    }

	public void setExperiment(String experiment) {
        this.experiment = experiment;
    }

	public String getOtherHours() {
        return this.otherHours;
    }

	public void setOtherHours(String otherHours) {
        this.otherHours = otherHours;
    }

	public String getWeeklyHours() {
        return this.weeklyHours;
    }

	public void setWeeklyHours(String weeklyHours) {
        this.weeklyHours = weeklyHours;
    }

	public Major getMajor() {
        return this.major;
    }

	public void setMajor(Major major) {
        this.major = major;
    }

	public ExaminationWay getExaminationWay() {
        return this.examinationWay;
    }

	public void setExaminationWay(ExaminationWay examinationWay) {
        this.examinationWay = examinationWay;
    }

	public Curriculum getCurriculum() {
        return this.curriculum;
    }

	public void setCurriculum(Curriculum curriculum) {
        this.curriculum = curriculum;
    }

	public Set<Semester> getSemester() {
        return this.semester;
    }

	public void setSemester(Set<Semester> semester) {
        this.semester = semester;
    }

	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

	@Version
    @Column(name = "version")
    private Integer version;

	public Long getId() {
        return this.id;
    }

	public void setId(Long id) {
        this.id = id;
    }

	public Integer getVersion() {
        return this.version;
    }

	public void setVersion(Integer version) {
        this.version = version;
    }

	@PersistenceContext
    transient EntityManager entityManager;

	public static final EntityManager entityManager() {
        EntityManager em = new LessonPlans().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

	public static long countLessonPlanses() {
        return entityManager().createQuery("SELECT COUNT(o) FROM LessonPlans o", Long.class).getSingleResult();
    }

	public static List<LessonPlans> findAllLessonPlanses() {
        return entityManager().createQuery("SELECT o FROM LessonPlans o", LessonPlans.class).getResultList();
    }

	public static LessonPlans findLessonPlans(Long id) {
        if (id == null) return null;
        return entityManager().find(LessonPlans.class, id);
    }

	public static List<LessonPlans> findLessonPlansEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM LessonPlans o", LessonPlans.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

	@Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

	@Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            LessonPlans attached = LessonPlans.findLessonPlans(this.id);
            this.entityManager.remove(attached);
        }
    }

	@Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

	@Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

	@Transactional
    public LessonPlans merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        LessonPlans merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }

	public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }
}
